package ru.niir.goodfil.audit.ui.extractors;

import ru.niir.goodfil.audit.domain.*;
import ru.niir.goodfil.audit.domain.enums.Crud;
import ru.niir.goodfil.audit.util.*;
import ru.niir.goodfil.excel.client.ValueExtractor;

/**
 * @author: Кирилл
 */
public class EventDescriptionExtractor implements ValueExtractor {
    public Object extract(Object o) {
        Assert.notNull(o);

        if (o instanceof EntityEvent) {
            return extractEntityEvent((EntityEvent) o);
        }
        if (o instanceof LoginEvent) {
            return extractLoginEvent((LoginEvent) o);
        }
        if (o instanceof EnviromentIntegrityEvent) {
            return extractEnviromentIntegrityEvent((EnviromentIntegrityEvent) o);
        }
        if (o instanceof ExceptionEvent) {
            return extractExceptionEvent((ExceptionEvent) o);
        }
        if (o instanceof SettingModificationEvent) {
            return extractSettingModificationEvent((SettingModificationEvent) o);
        }
        if (o instanceof UserUIEvent) {
            return extractUserUIEvent((UserUIEvent) o);
        }
        if (o instanceof ClientUIEvent) {
            return extractClientUIEvent((ClientUIEvent) o);
        }
        if (o instanceof ClientC1NumChangeEvent) {
            return extractClientC1NumChangeEvent((ClientC1NumChangeEvent) o);
        }
        if (o instanceof ClientDebtChangeEvent) {
            return extractClientDebtChangeEvent((ClientDebtChangeEvent) o);
        }
        if (o instanceof ClientDeliveryInfoChangeEvent) {
            return extractClientDeliveryInfoChangeEvent((ClientDeliveryInfoChangeEvent) o);
        }
        if (o instanceof ClientPriceColumnChangeEvent) {
            return extractClientPriceColumnChangeEvent((ClientPriceColumnChangeEvent) o);
        }
        if (o instanceof OrderUIEvent) {
            return extractOrderUIEvent((OrderUIEvent) o);
        }
        if (o instanceof OrderItemUIEvent) {
            return extractOrderItemUIEvent((OrderItemUIEvent) o);
        }

        return "";
    }

    private Object extractOrderItemUIEvent(OrderItemUIEvent o) {
        return "Заказ номер: " + o.getOrderId() + " \n" +
                "Товар: " + o.getGoodCode() + " [" + o.getGoodId() + "] \n" +
                "Кол-во: " + o.getCount() + " \n" +
                "Цена: " + o.getPrice();
    }

    private Object extractOrderUIEvent(OrderUIEvent o) {
        return "Заказ номер: " + o.getOrderId() + " \n" +
                "Номер 1С: " + o.getC1Num() + " \n" +
                "Дата создания: " + o.getCreationDate() + " \n" +
                "Дата доставки: " + o.getDeliveryDate() + " \n" +
                "Клиент: " + o.getClientName() + " [" + o.getClientId() + "] \n" +
                "Менедежр: " + o.getManagerName() + " [" + o.getManagerId() + "] \n" +
                "Комментарий: " + o.getOrderComment() + " \n" +
                "Стоимость: " + o.getOrderSum();
    }

    private Object extractClientPriceColumnChangeEvent(ClientPriceColumnChangeEvent o) {
        return o.getBrandName() + ": " + o.getOldColumnName() + " -> " + o.getNewColumnName();
    }

    private Object extractClientDeliveryInfoChangeEvent(ClientDeliveryInfoChangeEvent o) {
        return o.getDeliveryInfoOldString() + " -> " + o.getDeliveryInfoNewString();
    }

    private Object extractClientDebtChangeEvent(ClientDebtChangeEvent o) {
        return "Долг по банку: " + o.getOldDebtBank() + " -> " + o.getNewDebtBank() + ", Долг по кассе: " + o.getOldDebtCash() + " -> " + o.getNewDebtCash();
    }

    private Object extractClientC1NumChangeEvent(ClientC1NumChangeEvent o) {
        return o.getOldC1Num() + " -> " + o.getNewC1Num();
    }

    private Object extractClientUIEvent(ClientUIEvent o) {
        return "Наименование: " + o.getName() + ", " +
                "Код ИС: " + o.getClientId() + ", " +
                "Код 1С: " + o.getC1Num() + ", " +
                "Долг по банку: " + o.getDebtBank() + ", " +
                "Долг по кассе: " + o.getDebtCash() + ", " +
                "Менеджер: " + o.getManagerName();
    }

    private Object extractUserUIEvent(UserUIEvent o) {
        return "ФИО: " + o.getName() + ", " +
                "Дата создания: " + o.getCreationDate() + ", " +
                "Эл.почта: " + o.getMail() + ", " +
                "Таб.номер: " + o.getTabNumber() + ", " +
                "Роль: " + o.getRole() + ", " +
                "Логин: " + o.getUserLogin() + ", " +
                "Ид.:" + o.getUserId();
    }

    private String extractSettingModificationEvent(SettingModificationEvent o) {
        return "";
    }

    private String extractExceptionEvent(ExceptionEvent event) {
        return event.getStacktrace();
    }

    private String extractEnviromentIntegrityEvent(EnviromentIntegrityEvent event) {
        return "";
    }

    private String extractLoginEvent(LoginEvent o) {
        return "";
    }

    private String extractEntityEvent(EntityEvent e) {
        if (e.getCrud().equals(Crud.CREATE)) {
            StringBuilder sb = new StringBuilder();
            StringablePropertiesList properties = XMLUtils.getObjectXmlFromString(e.getXmlContent());
            for (StringableProperty property : properties.getItems()) {
                sb.append(property.getPropertyName() + ": \"" + property.getValue() + "\"\n");
            }
            return sb.toString();
        }
        if (e.getCrud().equals(Crud.DELETE)) {
            StringBuilder sb = new StringBuilder();
            StringablePropertiesList properties = XMLUtils.getObjectXmlFromString(e.getXmlContent());
            for (StringableProperty property : properties.getItems()) {
                sb.append(property.getPropertyName() + ": \"" + property.getValue() + "\"\n");
            }
            return sb.toString();
        }
        if (e.getCrud().equals(Crud.UPDARE)) {
            StringBuilder sb = new StringBuilder();
            StringablePropertyHistoriesList properties = XMLUtils.getObjectChangeXmlFromString(e.getXmlContent());
            for (StringablePropertyHistory property : properties.getItems()) {
                sb.append(property.getPropertyName() + ": ");
                sb.append("\"" + property.getOldValue() + "\" -> ");
                sb.append("\"" + property.getNewValue() + "\"\n");
            }
            return sb.toString();
        }

        Assert.exception();
        return "";
    }
}
